package com.gxc.controller;

import com.gxc.dto.AddUserParamsDto;
import com.gxc.dto.FindAllUserParamsDto;
import com.gxc.entity.User;
import com.gxc.service.UserService;
import com.gxc.utils.RestControllerResult;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author GongXincheng
 * @since 2019-08-08 14:48
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 添加测试数据.
     */
    @GetMapping("/addTest")
    @ResponseBody
    public RestControllerResult addTestUser() {
        List<User> userList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setUsername("admin_" + i);
            user.setPassword("pwd_" + i);
            user.setAge(i);
            user.setBirthday(new Date());
            user.setDescription("desc__" + i);
            userList.add(user);
        }
        Boolean data = userService.addTestUser(userList);
        return RestControllerResult.success(data);
    }

    /**
     * 添加用户.
     *
     * @param paramsDto 用户信息
     * @return 用户信息
     */
    @PostMapping("/addUser")
    @ResponseBody
    public RestControllerResult<User> addUser(
            @RequestBody @Valid AddUserParamsDto paramsDto, BindingResult bindingResult) {
        RestControllerResult<User> result = new RestControllerResult<>();
        if (bindingResult.hasErrors()) {
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            if (allErrors != null && allErrors.size() > 0) {
                result.setSuccess(false);
                result.setMessage(allErrors.get(0).getDefaultMessage());
                return result;
            }
        }
        User data = userService.addUser(paramsDto);
        result.setSuccess(true);
        result.setData(data);
        return result;
    }

    /**
     * 根据用户id查询用户信息.
     *
     * @param id 用户id
     * @return 用户信息
     */
    @GetMapping("/find/{id}")
    @ResponseBody
    public RestControllerResult<User> findById(@PathVariable Integer id) {
        User data = userService.findById(id);
        return RestControllerResult.success(data);
    }

    @PostMapping("/findAll")
    @ResponseBody
    public RestControllerResult<Page<User>> findAll(@RequestBody FindAllUserParamsDto paramsDto) {
        return RestControllerResult.success(userService.findAll(paramsDto));
    }

    @GetMapping("/test/valid")
    @ResponseBody
    public Object validToService() {
        AddUserParamsDto dto = new AddUserParamsDto();
        dto.setUsername(null);
        dto.setAge(200);
        dto.setPassword("123");
        return userService.validToService(dto);
    }

}
